package com.app.web.tools.controller;

import com.app.util.AjaxResult;
import com.app.web.BaseController;

public class ToolsController extends BaseController{
	//sql关键字
	private static String keyWords="insert into,delete,update,select,from,set , values,inner join,left join," +
			"right join,where , and , or , having , gorup by , order by , desc, asc";
	
	private static String[] ks=keyWords.split(",");

	
	//SQL格式化
	public void sqlFormat(){
		AjaxResult result = new AjaxResult(1,"格式化成功");
		String sql=this.getPara("sql","").toLowerCase();
		System.out.println(sql);
		try{
			if(sql.indexOf("select")!=-1){
				sql=sql.replace("select ", "\nselect\n\t");
			}
			if(sql.indexOf("delete")!=-1){
				sql=sql.replace("delete ", "delete\n\t");
			}
			if(sql.indexOf("insert into")!=-1){
				sql=sql.replace("insert into", "insert into\n\t");
			}
			if(sql.indexOf("update")!=-1){
				sql=sql.replace("update ", "update");
				String s=sql.substring(0, sql.indexOf(" "))+sql.substring(sql.indexOf(" "),sql.length());
				s=s.replace("update", "update ");
				s=s.replace("set", "\n\tset");
				sql=s;
			}
			if(sql.indexOf("from")!=-1){
				sql=sql.replace("from ", "\n\tfrom ");
			}
			if(sql.indexOf(" join ")!=-1){
				sql=sql.replace("inner join", "\ninner join");
				sql=sql.replace("left join", "\nleft join");
				sql=sql.replace("right join", "\nright join");
			}
			
			if(sql.indexOf("where")!=-1){
				sql=sql.replace("where ", "\nwhere ");
			}
			//将关键字装换成大写
			for(int i=0;i<ks.length;i++){
				if(sql.indexOf(ks[i])>-1){
					sql=sql.replaceAll(ks[i], ks[i].toUpperCase());
				}
			}
			result.setMsg(1, sql);
		}catch(Exception e){
			e.printStackTrace();
			result.setFailure("格式化失败");
		}
		
		System.out.println(sql);
		this.renderJson(result.toJson());
	}
	public static void main(String[] args) {
		String ssql="select a.aa,b.bb,a.cc from blog a inner join box b where 1=1 and 2=2 or 3=3 and 4=(select 4 from ddd where 1=1)";
		String usql="update blog set 1=1,2=2,3=3 where 1=1 and 2=(select 2 from kade where 2=2)";
		String dsql="delete from blog where 1=1 and 2=2";
		String isql="insert into blog values(select * from box);";
		
		//sqlFormat(dsql);
		int a=ssql.indexOf("select");
		System.out.println(a);
	}
}
